//
//  ViewController.m
//  Router
//
//  Created by Zhen Huang on 2017/2/7.
//  Copyright © 2017年 Zhen Huang. All rights reserved.
//

#import "ViewController.h"
#import "HHomeViewController.h"
#import "HLtcViewController.h"
#import "HQuotationViewController.h"
#import "HSetUpViewController.h"
#import <ReactiveCocoa.h>
#import "UIView+Layout.h"
#import "UtilsMacro.h"
#import "UIImage+Color.h"
#import "UIButton+ImageAndTitleAlignmentCenter.h"
#import "WCustomNavigationController.h"
#import "RegisterAndLoginViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *customTabber;
@property (nonatomic, strong) NSArray * titles;
@property (nonatomic, strong) NSArray * images;
@property (nonatomic, strong) NSArray * selectedImages;
@property (nonatomic, strong) NSMutableArray * buttons;
@property (nonatomic, strong) HHomeViewController *homeViewController;
@property (nonatomic, strong) HLtcViewController *LtcViewController;
@property (nonatomic, strong) HQuotationViewController *QuotationViewController;
@property (nonatomic, strong) HSetUpViewController *SetUpViewController;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [self tabForNative];
}
- (void)tabForNative {
    self.titles = @[@"首页",@"我的矿机",@"行情",@"设置"];
    self.images = @[@"zhuye1",@"kuangji1",@"hangqing1",@"shezhi1"];
    self.selectedImages = @[@"zhuye2",@"kuangji2",@"hangqing2",@"shezhi2"];
    self.buttons = [[NSMutableArray alloc]init];
    WCustomNavigationController *homeNav = [[WCustomNavigationController alloc]initWithRootViewController:self.homeViewController];
    WCustomNavigationController *homeNav1 = [[WCustomNavigationController alloc]initWithRootViewController:self.LtcViewController];
    WCustomNavigationController *homeNav2 = [[WCustomNavigationController alloc]initWithRootViewController:self.QuotationViewController];
    WCustomNavigationController *homeNav3 = [[WCustomNavigationController alloc]initWithRootViewController:self.SetUpViewController];
    
    self.viewControllers = @[homeNav,homeNav1,homeNav2,homeNav3];
    [self.tabBar addSubview:self.customTabber];
    [[UITabBar appearance] setShadowImage:[UIImage imageWithSolidColor:[UIColor clearColor] size:CGSizeMake(1, 1)]];
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithSolidColor:[UIColor clearColor] size:CGSizeMake(1, 1)]];
    self.tabBar.backgroundColor =[UIColor clearColor];
    self.selectedIndex = 0;
    @weakify(self);
    [RACObserve(self, selectedIndex)subscribeNext:^(id x) {
        @strongify(self);
        for (UIButton *button in self.buttons) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                button.enabled = [self.buttons indexOfObject:button]!=[x integerValue];
            });
        }
    }];
}
- (UIView *)customTabber {
    if (!_customTabber) {
        _customTabber = [[UIView alloc]initWithFrame:CGRectZero];
        _customTabber.width = self.tabBar.width;
        _customTabber.height = self.tabBar.height;
        _customTabber.left = 0;
        _customTabber.top = 0;
        
        UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 48)];
        backgroundView.backgroundColor = kBackGroundColor;
        [_customTabber addSubview:backgroundView];
        
        UIView *topLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.5)];
        topLine.backgroundColor = RGB(178, 178, 178);
        [_customTabber addSubview:topLine];
        
        for (NSInteger i = 0; i < self.viewControllers.count; i++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectZero];
            button.top = 3.5;
            button.height = self.tabBar.height;
            button.width = ScreenWidth/self.viewControllers.count;
            button.left = i*button.width;
            button.titleLabel.font = [UIFont systemFontOfSize:10];
            button.backgroundColor = kBackGroundColor;
            [button setTitleColor:RGB(119,119,119) forState:UIControlStateNormal];
            [button setTitleColor:RGB(119,119,119) forState:UIControlStateHighlighted];
            [button setTitleColor:kNavBlueColor forState:UIControlStateDisabled];
            [button setTitle:self.titles[i] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:self.images[i]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:self.images[i]] forState:UIControlStateHighlighted];
            [button setImage:[UIImage imageNamed:self.selectedImages[i]] forState:UIControlStateDisabled];
            [button setImageEdgeInsets:UIEdgeInsetsMake(20, 0, 12, 0)];
            
            @weakify(self);
            [[button rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
                @strongify(self);
                self.selectedIndex = i;
            }];
            [button centerImageAndTitle];
            [self.buttons addObject:button];
            [_customTabber addSubview:button];
        }
    }
    return _customTabber;
}

- (HHomeViewController *)homeViewController {
    HHomeViewController *homeVC = [[HHomeViewController alloc]init];
    homeVC.title = @"首页";
    return homeVC;
}
- (HLtcViewController *)LtcViewController {
    HLtcViewController *VC = [[HLtcViewController alloc]init];
    VC.title = @"我的矿机";
    return VC;
}
- (HQuotationViewController *)QuotationViewController {
    HQuotationViewController *VC = [[HQuotationViewController alloc]init];
    VC.title = @"行情";
    return VC;
}
- (HSetUpViewController *)SetUpViewController {
    HSetUpViewController *VC = [[HSetUpViewController alloc]init];
    VC.title = @"设置";
    return VC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
